
package com.bei_en.teacher.management.service.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.bei_en.teacher.management.model.system.bean.SystemUser;
import com.bei_en.teacher.management.model.system.bean.SystemUserRole;
import com.bei_en.teacher.management.model.system.condition.SystemUserCondition;
import com.bei_en.teacher.management.model.system.condition.SystemUserRoleCondition;
import com.bei_en.teacher.management.model.system.vo.SystemUserVo;
import com.bei_en.teacher.management.persistence.dao.system.SystemUserDao;
import com.bei_en.teacher.management.persistence.dao.system.SystemUserRoleDao;
import com.bei_en.teacher.management.service.BaseService;

/**
 *
 * @author Longqs
 * @date 2016-6-15 14:39:11
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class SystemUserService extends BaseService {

    @Autowired
    private SystemUserDao systemUserDao;
    @Autowired
    private SystemUserRoleDao systemUserRoleDao;
    @Autowired
    private SystemUserRoleService systemUserRoleService;

    public void insert(SystemUserVo model) {

	Integer id = merge(model);
	systemUserRoleDao.deletewhere(model.getId());
	String[] split = model.getUserRole().split(",");
	for (int i = 0; i<split.length; i++) {
	    SystemUserRole mm = new SystemUserRole();
	    mm.setUserId(id);
	    mm.setRoleId(Integer.valueOf(split[i]));
	    systemUserRoleDao.insert(mm);
	}
    }

    /**
     * 插入并返回生成的id
     * @param model
     * @return
     */
    public Integer merge(SystemUserVo model) {

	systemUserDao.insert(model.getSystemUser());
	return systemUserDao.findLastInsertedId();

    }

    public void delete(Integer modelInteger) {

	systemUserDao.delete(modelInteger);
    }

    public SystemUserVo findById(Integer modelInteger) {

	SystemUserVo model = new SystemUserVo(systemUserDao.findById(modelInteger));
	return model;
    }

    public void update(SystemUserVo model) {

	systemUserDao.update(model.getSystemUser());
    }

    /**
     * 动态更新,只更新非null的值
     * @param model
     */
    public void updateSelective(SystemUserVo model) {

	systemUserDao.updateSelective(model.getSystemUser());
	if (model.getStatus()!=0) {// 编辑或删除
	    systemUserRoleDao.deletewhere(model.getId());
	    if (!"".equals(model.getUserRole())) {
		String[] split = model.getUserRole().split(",");
		for (int i = 0; i<split.length; i++) {
		    SystemUserRole mm = new SystemUserRole();
		    mm.setUserId(model.getId());
		    mm.setRoleId(Integer.valueOf(split[i]));
		    systemUserRoleDao.insert(mm);
		}
	    }
	}
    }

    public int countAll() {

	return systemUserDao.countAll();
    }

    public List<SystemUserVo> findAll() {

	List<SystemUserVo> list = new ArrayList<SystemUserVo>();
	List<SystemUser> list1 = systemUserDao.findAll();
	for (SystemUser model : list1) {
	    list.add(new SystemUserVo(model));
	}
	return list;
    }

    public List<Integer> findAllIds() {

	return systemUserDao.findAllIds();
    }

    /**
     * 分页查询，包括总数和内容
     * @param Vo  返回总数
     * @return  返回内容
     */
    public List<SystemUserVo> findSystemUserPage(SystemUserCondition vo) {

	Integer totalCount = searchSystemUserNum(vo);
	List<SystemUserVo> list = new ArrayList<SystemUserVo>();
	vo.setTotalCount(totalCount);

	if (totalCount>0) {
	    vo.setOrderField(vo.getOrderField()!=null ? vo.getOrderField() : "user_id");
	    vo.setOrderDirection(vo.getOrderDirection()!=null ? vo.getOrderDirection() : "desc");

	    RowBounds rb = new RowBounds(vo.getStartIndex(), vo.getPageSize());
	    List<SystemUser> list1 = systemUserDao.findPageBreakByCondition(vo, rb);
	    for (SystemUser model : list1) {
		SystemUserRoleCondition vo1 = new SystemUserRoleCondition();
		vo1.setUserId(model.getUserId());
		String userRole = systemUserRoleService.findSystemUserRolePageString(vo1);
		SystemUserVo e = new SystemUserVo(model, userRole);
		list.add(e);
	    }

	}

	return list;
    }

    private Integer searchSystemUserNum(SystemUserCondition Vo) {

	return systemUserDao.findNumberByCondition(Vo);
    }

    public Map<String, String> findSysUserPageMap(SystemUserCondition vo) {

	Integer totalCount = searchSystemUserNum(vo);
	vo.setTotalCount(totalCount);
	Map<String, String> map = new HashMap<String, String>();

	if (totalCount>0) {
	    vo.setOrderField(vo.getOrderField()!=null ? vo.getOrderField() : "user_id");
	    vo.setOrderDirection(vo.getOrderDirection()!=null ? vo.getOrderDirection() : "desc");

	    RowBounds rb = new RowBounds(vo.getStartIndex(), vo.getPageSize());
	    List<SystemUser> list1 = systemUserDao.findPageBreakByCondition(vo, rb);
	    for (SystemUser model : list1) {
		map.put(model.getSalt(), model.getName());
	    }

	}

	return map;
    }

    public Map<String, String> findSysUserPageMap2(SystemUserCondition vo) {

	Integer totalCount = searchSystemUserNum(vo);
	vo.setTotalCount(totalCount);
	Map<String, String> map = new HashMap<String, String>();

	if (totalCount>0) {
	    vo.setOrderField(vo.getOrderField()!=null ? vo.getOrderField() : "user_id");
	    vo.setOrderDirection(vo.getOrderDirection()!=null ? vo.getOrderDirection() : "desc");

	    RowBounds rb = new RowBounds(vo.getStartIndex(), vo.getPageSize());
	    List<SystemUser> list1 = systemUserDao.findPageBreakByCondition(vo, rb);
	    for (SystemUser model : list1) {
		map.put(model.getUserId().toString(), model.getName());
	    }

	}

	return map;
    }

}
